//
//  PagedCollectionView.swift
//  LiveShow
//
//  Created by dai zhong on 2017/4/17.
//  Copyright © 2017年 company.cn. All rights reserved.
//

import UIKit

protocol PagedCollectionViewDataSource:class {
    func numberOfSections(in pagedCollectionView: PagedCollectionView) -> Int;
    func pagedCollectionView(_ pagedCollectionView: PagedCollectionView, numberOfItemsInSection section: Int) -> Int;
    func pagedCollectionView(_ pagedCollectionView: PagedCollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell;
}

// PagedCollectionViewDataSource 的 默认实现
extension PagedCollectionViewDataSource{

    func numberOfSections(in pagedCollectionView: PagedCollectionView) -> Int{
        return 1
    }
}

@objc protocol PagedCollectionViewDelegate:class {
    @objc optional func pagedCollectionView(_ pagedCollectionView: PagedCollectionView, didSelectItemAt indexPath: IndexPath);
    @objc optional func pagedCollectionView(_ pagedCollectionView: PagedCollectionView, didScrolledPageAt indexPath: IndexPath);
    @objc optional func pagedCollectionView(_ pagedCollectionView: PagedCollectionView);
}

class PagedCollectionView: UIView {
    
    open weak var dataSource:PagedCollectionViewDataSource?;
    open weak var delegate:PagedCollectionViewDelegate?;
    open var contentOffset :CGPoint?;
    
    fileprivate var style : PagedCollectionStyle;
    fileprivate var layout:PagedCollectionViewLayout;

    fileprivate lazy var collectionView :UICollectionView = {
        
        let collectionView :UICollectionView = UICollectionView(frame: self.bounds, collectionViewLayout: self.layout);
        collectionView.showsVerticalScrollIndicator = false;
        collectionView.showsHorizontalScrollIndicator = self.style.showsHorizontalScrollIndicator;
        collectionView.isPagingEnabled = self.style.isPagingEnabled;
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kPagedCollectionCellIdentifier)
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = self.style.backgroundColor;
        return collectionView;
    }()
    
    fileprivate lazy var pageControl :UIPageControl = {
    
        let pageControl = UIPageControl();
        pageControl.frame = CGRect(x: 0, y: self.bounds.height - self.style.pageControlHeight , width: self.bounds.width, height: self.style.pageControlHeight);
        pageControl.numberOfPages = 2;
        pageControl.currentPage = 0;
        pageControl.hidesForSinglePage = self.style.hidesForSinglePage;
        pageControl.currentPageIndicatorTintColor = self.style.currentPageIndicatorTintColor;
        pageControl.pageIndicatorTintColor = self.style.pageIndicatorTintColor;
        return pageControl;
    }();
    
    init(pagedCollectionFrame frame: CGRect ,pagedCollectionStyle style :PagedCollectionStyle) {
        
        self.style = style;
        self.layout = PagedCollectionViewLayout(pageCollectionStyle: style);
        super.init(frame: frame);
        setupUI();
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK:- Public Method
extension PagedCollectionView{

    public func registerCell(_ cellClass: Swift.AnyClass?, forCellWithReuseIdentifier identifier: String){
        collectionView.register(cellClass, forCellWithReuseIdentifier: identifier);
    }
    
    public func registerCellFromNib(_ nib: UINib?, forCellWithReuseIdentifier identifier: String){
        collectionView.register(nib, forCellWithReuseIdentifier: identifier);
    }
    
    public func reloadData(){
        collectionView.reloadData();
    }
    
    public func dequeueReusableCell(withReuseIdentifier identifier: String, for indexPath: IndexPath) -> UICollectionViewCell{
    
        return collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath);
    }
    
    public func selectItemAtPath(didSelectItemAt indexPath: IndexPath,animated:Bool){
    
        
        let point = self.layout.scrollToItem(at: (indexPath as NSIndexPath) as IndexPath);
        collectionView.setContentOffset(point, animated: animated);
    }
}

// MARK:- setupUI
extension PagedCollectionView{

    fileprivate func setupUI(){
    
        self.addSubview(collectionView);
        
        if self.style.isShowPageControl {
           self.addSubview(pageControl);
        }
    }
    
    fileprivate func scrollViewDidEndScrolling(_ scrollView: UIScrollView){
    
        let cell = collectionView.visibleCells.first;
        guard let targetCell = cell else {
            return;
        }
        let temp = collectionView.indexPath(for: targetCell);
        guard let indexPath = temp else {
            return;
        }
        self.scrollViewDidEndScrollingToAtPath(didEndScrolledAt: indexPath);
    }
    fileprivate func scrollViewDidEndScrollingToAtPath(didEndScrolledAt indexPath: IndexPath){
    
        let numberOfPages = self.layout.pageCountInSection(InSection: indexPath.section);
        let currentPage = self.layout.itemPageIndexInSection(at: (indexPath as NSIndexPath) as IndexPath);
        if self.style.isShowPageControl {
            
            pageControl.numberOfPages = numberOfPages
            pageControl.currentPage = currentPage
        }
        let pageIndexPath = IndexPath(item: currentPage, section: indexPath.section);
        self.delegate?.pagedCollectionView!(self, didScrolledPageAt: pageIndexPath as IndexPath);
    }
}

// MARK:- 重写数据源方法
extension PagedCollectionView: UICollectionViewDataSource{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return self.dataSource?.numberOfSections(in: self) ?? 0;
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource?.pagedCollectionView(self, numberOfItemsInSection: section) ?? 0;
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        return (self.dataSource?.pagedCollectionView(self, cellForItemAt: indexPath))!;
    }
}

// MARK:- 重写代理方法
extension PagedCollectionView:UICollectionViewDelegate{

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        self.delegate?.pagedCollectionView!(self, didSelectItemAt: indexPath);
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        self.scrollViewDidEndScrolling(scrollView);
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        self.scrollViewDidEndScrolling(scrollView);
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.contentOffset?.x = scrollView.contentOffset.x;
        self.contentOffset?.y = scrollView.contentOffset.y;
        self.delegate?.pagedCollectionView!(self);
    }
}


















































